<?php

class Wise_View extends Zend_View
{
	/**
	 * Params like (action, controller, module)
	 *
	 * @var array
	 */
	private $params = array();
	
	/**
	 * Instance of Zend_Translate_Adapter
	 *
	 * @var Zend_Translate_Adapter
	 */
	private $translate = null;
	
	/**
	 * Set initial data
	 */
	protected function _run()
	{
		$request = Zend_Controller_Front::getInstance()->getRequest();

		$this->params['action']		= $request->getActionName();
		$this->params['controller']	= $request->getControllerName();
		$this->params['module']		= $request->getModuleName();
		
		$this->translate = Zend_Registry::get('Zend_Translate');
		
		parent::_run(func_get_arg(0));
	}
	
	/**
	 * Quote values while outputing them in template
	 *
	 * @param string $var value to quote
	 * @return string quoted value
	 */
	public function qtext($var)
    {
    	return $this->escape($var);
    }
    
    /**
     * Translates provided message Id
     * 
     * You can give multiple params or an array of params.
     * If you want to output another locale just set it as last single parameter
     * Example 1: translate('%1\$s + %2\$s', $value1, $value2, $locale);
     * Example 2: translate('%1\$s + %2\$s', array($value1, $value2), $locale);
     *
     * @param  string $messageid Id of the message to be translated
     * @return string Translated message
     */
    public function _($messageid = null)
    {
    	/**
    	 * Process the arguments
    	 */
        $options = func_get_args();
        array_shift($options);

        $count  = count($options);
        $locale = null;
        
        if ($count > 0) {
            if (Zend_Locale::isLocale($options[($count - 1)], null, false) !== false) {
                $locale = array_pop($options);
            }
        }

        if ((count($options) === 1) and (is_array($options[0]) === true)) {
            $options = $options[0];
        }
        
        /**
         * Proxify the call to Zend_Translate_Adapter
         */
        $message = $this->translate->translate($messageid, $locale);
        
        /**
         * If no any options provided then just return message 
         */
        if ($count === 0) {
            return $message;
        }
        
        /**
         * Apply options in case we have them
         */
        return vsprintf($message, $options);
    }
    
	/**
	 * Returns parameter requested in template 
	 * 
	 * @param string $param param name
	 * @return string param value
	 */
    public function isRoute($module, $controller = null , $action = null)
    {
		if (null !== $action) {
			
			if ($module === $this->params['module'] 
				&& $controller === $this->params['controller']
				&& $action === $this->params['action']) {
				return true;
			} else {
				return false;
			}
			
		}
		
		if (null !== $controller) {
			
			if ($module === $this->params['module'] 
				&& $controller === $this->params['controller']) {
				return true;
			} else {
				return false;
			}
			
		}
		
		if ($module === $this->params['module']) {
			return true;
		}
		
		return false;
    }
    
	/**
	 * Calculates and outputs the translation tags cloud
	 * 
	 * @param $tags
	 * @return string
	 */
	public function TranslationsCloud($tags)
	{
		if (empty($tags)) {
			return;
		}
		
		/**
		 * All tags will be devided into six groups
		 */
		$tag_groups = array(
			  1 => 'not-popular'
			, 2 => 'not-very-popular'
			, 3 => 'somewhat-popular'
			, 4 => 'popular'
			, 5 => 'very-popular'
			, 6 => 'ultra-popular'
		);
		
		/**
		 * Calculat the about of tags for each grup
		 */
		$num_of_tags = count($tags);
		
		$tags_in_group = $num_of_tags / count($tag_groups);
		$tags_for_group_index = 1 / $tags_in_group;
		
		/**
		 * Build the ordered tags list 
		 */
		$tags_cloud = array();
		foreach ($tags as $tag) {
			$tags_cloud[] = $tag;
		}
		
		/**
		 * Assign each tag to particular group
		 */
		$processed_tags_cloud = array();
		$group_index = 0;
		foreach ($tags_cloud as $position => $tag) {
		
			$group_index = $group_index + $tags_for_group_index;
			
			$group_id = ceil($group_index);
			$tag['group'] = ($group_id > count($tag_groups)) ? count($tag_groups) : $group_id;
			
			$processed_tags_cloud[] = $tag;
		}
		
		/**
		 * Randomize tags to cuild the cloud
		 */
		shuffle($processed_tags_cloud);
		
		
		/**
		 * Output the tags cloud
		 */
		?>
		<div id="popular searches">
			<h2><?php echo  $this->_('Popular search phrases'); ?></h2>
			<div class="tag-cloud">
			<?php foreach ($processed_tags_cloud as $tag): ?>
				<div class="tag <?php echo $tag_groups[$tag['group']] ?>">
					<span><?php $this->_('Users have searched %1\$s times for ', $tag['num']) ?></span>
					<a rel="tag" href="javascript:applyTag('<?php echo $tag['phrase'] ?>')"><?php echo $tag['phrase'] ?></a>
				</div>
			<?php endforeach; ?>
			</div>
		</div>
		<?php 
	}

}